% function estimaLoopSingle
%% 7. Begin Estimation Loop
disp('Check ParStrvals....');

if flags.parallel==0
    loopTable=emptycell(numpar+7,priorStru.numStartVals+1);
    loopTable(1:numpar,1)=parnames;
    loopTable(numpar+2:end,1)={'logPost';'logLikel';'Number';'Iteration count';'exitflag';'minutes'};
    cd(location.savePath);save loopTable;cd(cucd);
end

for ii=1:priorStru.numStartVals;
    
    %% 7a) Parvecmode is the mode
    parVecInitial=zeros(numpar,1);
    parVecInitial(posStru.param.cal)=parStru.cal;
    
    parStru.param=loopStru.parStartMat(:, ii);
        
    %% 7b) Optimization Routine, output is optimOut
    disp(['Starting Value',num2str(ii)]);
    optimOut=feval(estimopt.funcHandle,loopStru.parStartMat(posStru.param.est,ii),estimopt,...
        parStru,posStru,model,dataStru,prpar,prss,filterStru,flags,struInOpt);
    
    %% 7c) Assing fields of optimOut
    parVecPosterior=zeros(numpar,1);
    parVecPosterior(posStru.param.cal) =parStru.cal;
    
    parVecPosterior(posStru.param.est)= optimOut.mode;
    %parestmat(:,ii)=optimOut.mode;
    loop_parMode(posStru.param.cal,ii)=parStru.cal;
    loop_parMode(:,ii)=parVecPosterior;
    loop_std(:,ii)=sqrt(diag(optimOut.Hessian));
    loop_hessian(:,:,ii)=optimOut.Hessian;
    
    %% 7d) Compute Posterior (old syntaxis, replace by passing structure)
    [loop_logPost(ii,2),loop_logLikel(ii,2),loop_logPrior(ii,2)]=...
        feval(filterStru.funcPost,optimOut.mode,...
        parStru,posStru,model,dataStru,prpar,prss,filterStru,flags);
    
    if isinf( abs(loop_logPost(ii,2) ) ) ==false && isnan(loop_logPost(ii,2) ) ==false
        if abs( -optimOut.LPosterior-loop_logPost(ii,2) ) > 1e-4
            warning('Posterior from Post and Min do not match!!!!!!!!!!!!!!!!!!')
        end
        if abs( loop_logPrior(ii,2) - ( loop_logPost(ii,2)-loop_logLikel(ii,2)  )  ) > 1e-4
            warning('Prior is NOT computed correctly!!!!!!!!!!!!!!!!');
        end
    end
    loop_exitFlag(ii)=optimOut.exitFlag;
    loop_iterations(ii)=optimOut.iterations;
    loop_time(ii)=optimOut.time/60;
    
    
    %% 7c) Fill in Tab_loop ONLY for Paralell
    if flags.parallel==0
        loopTable(1:numpar,ii+1)=num2cprec(loop_parMode(:,ii),15);
        loopTable{numpar+2,ii+1}=num2str(loop_logPost(ii,2));
        loopTable{numpar+3,ii+1}=num2str(loop_logLikel(ii,2));
        loopTable{numpar+4,ii+1}=num2str(ii);
        loopTable{numpar+5,ii+1}=num2str(optimOut.iterations);
        loopTable{numpar+6,ii+1}=num2str(optimOut.exitFlag);
        loopTable{numpar+7,ii+1}=num2str(optimOut.time);
        
        %% Try to save to save to a mat and XLSFILE
        try
            cd(location.savePath);
            save mat_loop loop_logPrior loop_logPost loop_logPrior ...
                loop_parMode loop_exitFlag loop_iterations loop_time loop_logLikel
            save workspace
            if ~isunix
                xlswrite('loopTable', loopTable);
            end
            cd(cucd);
        catch
            disp('Could not save the data')
        end
        
    end
    
end
